package com.spica.platform.base.validator.constraint;

import com.spica.platform.base.validator.annotion.IdValid;
import com.spica.platform.common.utils.StringUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * ID校验器，主要判断是否为空，并且长度是否为32-long类型
 */
public class IdValidator implements ConstraintValidator<IdValid, String> {
    private static final int LONG_LENGTH = 32;

    @Override
    public void initialize(IdValid constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null || StringUtil.isBlank(value) || StringUtil.isEmpty(value.trim()) || value.length() != LONG_LENGTH) {
            return false;
        }
        return true;
    }
}
